package com.pignest.hotel.ui.room;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.definition.RoomLiveStatus;
import com.pignest.hotel.definition.RoomRepairStatus;
import com.pignest.hotel.definition.RoomUseStatus;
import com.pignest.hotel.service.RoomService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.L;
import com.pignest.hotel.util.UIUtils;

/**
 * 增加或编辑房间窗口
 * */
public class RoomWindow extends Window {
	private static final long serialVersionUID = 2147239767923279155L;
	
	private boolean add;	//or edit
	private String roomId;
//	private Room room;
	private RoomService service;
	
	private Textbox startNumberTextbox, endNumberTextbox;
	private Listbox roomTypeList;
	private Listbox floorList;
	private Listbox selfHelpList;
	private Listbox colorList;
	private Textbox remarkTextbox;
	private Textbox lockNumberTextbox;
	private boolean success;
	
	public RoomWindow() {
		service = Services.getRoomService();
		add = true;
	}
	
	private void init(Room room) {
		if (startNumberTextbox != null)
			return;
		startNumberTextbox = (Textbox)getFellow("startNumber");
		endNumberTextbox = (Textbox)getFellow("endNumber");
		roomTypeList = (Listbox)getFellow("roomType");
		floorList = (Listbox)getFellow("floor");
		selfHelpList = (Listbox)getFellow("selfHelp");
		colorList = (Listbox)getFellow("color");
		lockNumberTextbox = (Textbox)getFellow("lockNumber");
		remarkTextbox = (Textbox)getFellow("remark");
		if (!add) {
			endNumberTextbox.setReadonly(true);
			endNumberTextbox.setVisible(false);
			startNumberTextbox.setValue(room.getName());
			UIUtils.setSelectedItem(roomTypeList, room.getType().getId());
			UIUtils.setSelectedItem(floorList, room.getFloor());
			UIUtils.setSelectedItem(selfHelpList, String.valueOf(room.isSelfHelp()));
			UIUtils.setSelectedItem(colorList, room.getColor());
			lockNumberTextbox.setValue(room.getLockNumber());
			remarkTextbox.setValue(room.getRemark());
		}
	}
	
	public String onSave() {
		success = false;
		String err = check();
		if (err != null)
			return err;
		String startNumber = startNumberTextbox.getValue().trim();
		String roomTypeId = (String)UIUtils.getSelectedItem(roomTypeList).getValue();
		String floor = (String)UIUtils.getSelectedItem(floorList).getValue();
		String remark = StringUtils.defaultString(remarkTextbox.getValue(), null);
		boolean selfHelp = "true".equals(UIUtils.getSelectedValue(selfHelpList));
		String color = StringUtils.defaultIfEmpty((String)UIUtils.getSelectedValue(colorList), null);
		String lockNumber = StringUtils.defaultIfEmpty(lockNumberTextbox.getValue(), null);
		Room r = lockNumber == null ? null : Services.getRoomService().getRoomByLock(lockNumber);
		if (add) {
			String endNumber = endNumberTextbox.getValue();
			if (StringUtils.isNotEmpty(endNumber)) {
				int start = Integer.parseInt(startNumber);
				int end = Integer.parseInt(endNumber);
				for (int i = start; i <= end; i++) {
					Room room = new Room();
					room.setName(String.valueOf(i));
					room.setType(service.getRoomType(roomTypeId));
					room.setFloor(floor);
					room.setSelfHelp(selfHelp);
					room.setColor(color);
					room.setRoomUseStatus(RoomUseStatus.DEFAULT);
					room.setRoomLiveStatus(RoomLiveStatus.DEFAULT);
					room.setRoomRepairStatus(RoomRepairStatus.NORMAL);
					room.setRemark(remark);
					if (start == end)
						room.setLockNumber(lockNumber);
					service.addRoom(room);
					L.insert(room);
				}
			} else {
				if (r != null)
					return "锁号已经被房间占用: " + r.getName();
				Room room = new Room();
				room.setName(startNumber);
				room.setType(service.getRoomType(roomTypeId));
				room.setFloor(floor);
				room.setSelfHelp(selfHelp);
				room.setColor(color);
				room.setRoomUseStatus(RoomUseStatus.DEFAULT);
				room.setRoomLiveStatus(RoomLiveStatus.DEFAULT);
				room.setRoomRepairStatus(RoomRepairStatus.NORMAL);
				room.setRemark(remark);
				room.setLockNumber(lockNumber);
				service.addRoom(room);
				L.insert(room);
			}
		} else {
			Room room = null;
			if (r != null)
				if (r.getId().equals(roomId))
					room = r;
				else
					return "锁号已经被房间占用: " + r.getName();
			else
				room = service.getRoom(roomId);
			room.setName(startNumber);
			room.setType(service.getRoomType(roomTypeId));
			room.setFloor(floor);
			room.setSelfHelp(selfHelp);
			room.setColor(color);
			room.setRemark(remark);
			room.setLockNumber(lockNumber);
			service.updateRoom(room);
			L.update(room);
		}
		success = true;
		return null;
	}
	
	private String check() {
		if (UIUtils.getSelectedIndex(roomTypeList) == -1)
			return "请选择一个房间类型";
		if (UIUtils.getSelectedIndex(floorList) == -1)
			return "请选择房间所属楼层";
		String startNumber = startNumberTextbox.getValue();
		if (StringUtils.isBlank(startNumber))
			return "房间号必填";
		String lockNumber = lockNumberTextbox.getValue();
		if (add) {
			String endNumber = endNumberTextbox.getValue().trim();
			if (StringUtils.isNotEmpty(endNumber)) {
				if (endNumber.length() != startNumber.length())
					return "开始房间号和结束房间长度不一致";
				int start = Utils.getInteger(startNumber, -1);
				int end = Utils.getInteger(endNumber, -1);
				if (start <= 0 || end <= 0 || start > end)
					return "房间号填写不合法";
				if (end != start && StringUtils.isNotEmpty(lockNumber))
					return "批量增加房间时请不要设置锁号";
			}
		}
		return null;
	}
	
	public void setRoomId(String roomId) {
		this.roomId = roomId;
		Room room = StringUtils.isEmpty(roomId) ? null : service.getRoom(roomId);
		if (room != null) {
			this.add = false;
			setTitle("编辑房间");
		} else {
			this.add = true;
			setTitle("新增房间");
		}
		init(room);
	}
	
	public boolean isSuccess() {
		return success;
	}
}
